package org.celery.modules.ticket.enums;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 交易状态的枚举类
 */

@Getter
@AllArgsConstructor
public enum transactionStatusEnum {

    /**
     * 等待商家付款
     */
    WAITING_FOR_BUYER_PAYMENT("等待买家付款",  "waitbuyerpay"),
    WAITING_FOR_SELLER_TO_SHIP("等待卖家发货",  "waitsellersend"),
    WAITING_FOR_THE_LOGISTICS_COMPANY_TO_PICK_UP_THE_PACKAGE("等待物流公司揽件",  "waitlogisticstakein"),
    WAITING_FOR_THE_BUYER_TO_RECEIVE_THE_GOODS("等待买家收货",  "waitbuyerreceive"),
    WAITING_FOR_BUYER_TO_SIGN_FOR_RECEIPT("等待买家签收",  "waitbuyersign"),
    THE_BUYER_HAS_SIGNED_FOR_IT("买家已签收",  "signinsuccess"),
    RECEIVED_GOODS("已收货",  "confirm_goods"),
    SUCCESSFUL_TRANSACTION("交易成功",  "success"),
    TRANSACTION_CANCELLATION("交易取消",  "cancel"),
    TRANSACTION_TERMINATION("交易终止",  "terminated"),
    ;

    private String name;
    private String value;

    public static String getName(String value) {
        if (value.equals("waitbuyerpay")) {
            return WAITING_FOR_BUYER_PAYMENT.getName();
        } else if (value.equals("waitsellersend")) {
            return WAITING_FOR_SELLER_TO_SHIP.getName();
        } else if (value.equals("waitlogisticstakein")) {
            return WAITING_FOR_THE_LOGISTICS_COMPANY_TO_PICK_UP_THE_PACKAGE.getName();
        } else if (value.equals("waitbuyerreceive")) {
            return WAITING_FOR_THE_BUYER_TO_RECEIVE_THE_GOODS.getName();
        } else if (value.equals("waitbuyersign")) {
            return WAITING_FOR_BUYER_TO_SIGN_FOR_RECEIPT.getName();
        } else if (value.equals("signinsuccess")) {
            return THE_BUYER_HAS_SIGNED_FOR_IT.getName();
        } else if (value.equals("confirm_goods")) {
            return RECEIVED_GOODS.getName();
        } else if (value.equals("success")) {
            return SUCCESSFUL_TRANSACTION.getName();
        } else if (value.equals("cancel")) {
            return TRANSACTION_CANCELLATION.getName();
        } else if (value.equals("terminated")) {
            return TRANSACTION_TERMINATION.getName();
        }
        return "";
    }

    public static String getValue(String value) {
        if (WAITING_FOR_BUYER_PAYMENT.getValue().equals(value)) {
            return WAITING_FOR_BUYER_PAYMENT.getValue();
        } else if (WAITING_FOR_SELLER_TO_SHIP.getValue().equals(value)) {
            return WAITING_FOR_SELLER_TO_SHIP.getValue();
        } else if (WAITING_FOR_THE_LOGISTICS_COMPANY_TO_PICK_UP_THE_PACKAGE.getValue().equals(value)) {
            return WAITING_FOR_THE_LOGISTICS_COMPANY_TO_PICK_UP_THE_PACKAGE.getValue();
        } else if (WAITING_FOR_THE_BUYER_TO_RECEIVE_THE_GOODS.getValue().equals(value)) {
            return WAITING_FOR_THE_BUYER_TO_RECEIVE_THE_GOODS.getValue();
        } else if (WAITING_FOR_BUYER_TO_SIGN_FOR_RECEIPT.getValue().equals(value)) {
            return WAITING_FOR_BUYER_TO_SIGN_FOR_RECEIPT.getValue();
        } else if (THE_BUYER_HAS_SIGNED_FOR_IT.getValue().equals(value)) {
            return THE_BUYER_HAS_SIGNED_FOR_IT.getValue();
        } else if (RECEIVED_GOODS.getValue().equals(value)) {
            return RECEIVED_GOODS.getValue();
        } else if (SUCCESSFUL_TRANSACTION.getValue().equals(value)) {
            return SUCCESSFUL_TRANSACTION.getValue();
        } else if (TRANSACTION_CANCELLATION.getValue().equals(value)) {
            return TRANSACTION_CANCELLATION.getValue();
        } else if (TRANSACTION_TERMINATION.getValue().equals(value)) {
            return TRANSACTION_TERMINATION.getValue();
        }
        return "";
    }

    public JSONObject toJSON() {
        JSONObject jo = new JSONObject();
        jo.put("name", name);
        jo.put("value", value);
        return jo;
    }

    public static List<Map<String, Object>> toList() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (transactionStatusEnum item : transactionStatusEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", item.getName());
            map.put("value", item.getValue());
            list.add(map);
        }
        return list;
    }
}
